<?php

/* 会员 buyer */
class BuyerModel extends BaseModel
{
    var $table  = 'buyer';
    var $prikey = 'buyer_id';
    var $_name  = 'buyer';
	
	
    var $_relation = array(
		'belongs_merchandiser' => array(
            'model'         => 'member',
            'type'          => BELONGS_TO,
            'foreign_key'   => 'user_id',
            'reverse'       => 'has_buyer',
        ),
		'belongs_company' => array(
            'model'         => 'company',
            'type'          => BELONGS_TO,
            'foreign_key'   => 'company_id',
            'reverse'       => 'has_buyer',
        ),
		'has_sample' => array(
			'model' => 'sample',
			'type' => HAS_MANY,
			'foreign_key' => 'buyer_id',
		),
	);

    /*
     * 判断名称是否唯一
     */
    function unique($buyer_name, $buyer_id = 0)
    {
        $conditions = "buyer_name = '" . $buyer_name . "'";
        $buyer_id && $conditions .= " AND buyer_id <> '" . $buyer_id . "'";
        return count($this->find(array('conditions' => $conditions))) == 0;
    }
}

?>